<?php
class MyorderModule
{
	function index(){
		global $_G;
		if($_G['users']['shopid']){
			error('拒绝访问',url('home.php'));
		}
		$action = isset($_GET['a'])?$_GET['a']:null;
		$lang = lang::load('user');
		$lang = lang::load('point');
		$orderstatus = array(
			'0' => '等待确认',
			'1' => '打包中',
			'4' => '外送中',
			'7' => '已完成',
			'9'	=> '已取消',
		
		);
		$status=array('0'=>'未支付','1'=>'等待确认','2'=>'正在配货', '4'=>'正在配送','7'=>'已完成','9'=>'已取消');
		switch($action) {
			case 'new':
				$limit = 15;
				$caidans = $datas = $ids = array();
				$field = getGP('search_field','P');
				$where = '';
				if($field) {
					$where = " AND (o.txnid='".$field."' OR s.phone LIKE '%".$field."%') ";
				}
				$sql = 'SELECT o.*,s.firstname,s.phone,s.street,s.shipping_fee FROM'.table('orders').'o LEFT JOIN'.table('orders_shipping').'s ON o.txnid=s.txnid WHERE o.touid="'.$_G['users']['shopid'].'"'.$where;
				$query = $_G['db']->page_query($sql,$limit);
				$num = $_G['db']->result('SELECT count(o.txnid) FROM'.table('orders').'o LEFT JOIN'.table('orders_shipping').'s ON o.txnid=s.txnid WHERE o.touid="'.$_G['users']['shopid'].'"'.$where);
				while($data = $_G['db']->fetch_array($query)) {
					$datas[] = $data ;
					$ids[] = $data['txnid'];
				}
				$query = $_G['db']->query('SELECT * FROM'.table('orders_product').'WHERE txnid IN('.implode(',', $ids).')');
				while($data = $_G['db']->fetch_array($query)){
					$caidans[$data['txnid']][] = $data;
				}
				$pager = pager($num,$limit);
				break;
			case 'increase':
				$id = getGP('id','G','int');
				$txnid = getGP('txnid','G','int');
				$orders = $_G['db']->fetch_one_array('SELECT o.*,s.firstname,s.phone,s.street,s.shipping_fee FROM'.table('orders').'o LEFT JOIN'.table('orders_shipping').'s ON o.txnid=s.txnid WHERE o.txnid="'.$txnid.'" AND o.uid="'.$_G['uid'].'"');
				//权限检查
				if(!$id || !$txnid || !$orders['txnid']){
					$return['s'] = false;
					$return['m'] = '';
					print_r(json_encode($return));
					exit;
				}
				$products = $_G['db']->fetch('orders_product','*', array('txnid' =>$txnid,'pid'=>$id));
				$good = $_G['db']->fetch('caidan','price,stock,shopid,pid,title,image',array('status'=>1,'pid'=>$id));
				if( 1 > $good['stock']){//不存在或者库存为0
					$return['s'] = false;
					$return['m'] = '';
				}elseif($good['pid']){
					if(!empty($products['pid'])){
						$_G['db']->update('orders_product',array('qty'=>$products['qty']+1),array('pid'=>$id,'txnid'=>$txnid));
						$_G['db']->update('orders',array('gross'=>$orders['gross'] + $products['price']),array('txnid'=>$txnid));
						$products['qty'] +=1;
					}
					$return['s'] = true;
					$return['q'] = $products['qty'];
					$return['p'] = $products['qty'] * $products['price'];
					$return['t'] = $orders['gross'] + $products['price'];
				}
				print_r(json_encode($return));
				exit;
				break;
			case 'decrease':
				$id = getGP('id','G','int');
				$txnid = getGP('txnid','G','int');
				$orders = $_G['db']->fetch_one_array('SELECT o.*,s.firstname,s.phone,s.street,s.shipping_fee FROM'.table('orders').'o LEFT JOIN'.table('orders_shipping').'s ON o.txnid=s.txnid WHERE o.txnid="'.$txnid.'" AND o.uid="'.$_G['uid'].'"');
				//权限检查
				if(!$id || !$txnid || !$orders['txnid']){
					$return['s'] = false;
					$return['m'] = '';
					print_r(json_encode($return));
					exit;
				}
				$products = $_G['db']->fetch('orders_product','*', array('txnid' =>$txnid,'pid'=>$id));
				$good = $_G['db']->fetch('caidan','price,stock,shopid,pid,title,image',array('status'=>1,'pid'=>$id));
				
				if( !$good['pid']){//不存在
					$return['s'] = false;
					$return['m'] = '';
				}elseif($good['pid']){
					$shop = $_G['db']->fetch('shops','minprice,minnum,extprice',array('shopid'=>$good['shopid']));
					if($orders['gross'] - $products['price']<$shop['minprice']){
						$return['s'] = false;
						$return['m'] = '操作失败，原因：将导致订单总价少于最少起送价'.$shop['minprice'].'元';
					}else{
					if($products['qty']-1 >=0){
						if(!empty($products['pid'])){
							if($products['qty'] == 1){
								$_G['db']->query('DELETE FROM '.table('orders_product').'WHERE pid="'.$id.'" AND txnid="'.$txnid.'"');
							}else{
								$_G['db']->update('orders_product',array('qty'=>$products['qty']-1),array('pid'=>$id,'txnid'=>$txnid));
							}
							$_G['db']->update('orders',array('gross'=>$orders['gross'] - $products['price']),array('txnid'=>$txnid));
							$products['qty'] -=1;
						}
						$return['s'] = true;
						$return['q'] = $products['qty'];
						$return['p'] = $products['qty'] * $products['price'];
						$return['t'] = $orders['gross'] - $products['price'];
					}
					}
				}
				print_r(json_encode($return));
				exit;
				break;
			case 'cancel':
				$id = getGP('id','G','int');
				$return['s'] = false;
				$return['m'] = '';
				if(!$id){
					json($return);
				}
				$orders = $_G['db']->fetch('orders','txnid,status' ,array('txnid'=>$id,'uid'=>$_G['uid']));
				if(!$orders['txnid']){
					json($return);
				}
				$_G['db']->update('orders',array('status'=>9),array('txnid'=>$id));
				$return['s'] = true;
				$return['m'] = '操作成功';
				json($return);
				break;	
			case 'clear':
				$id = getGP('id','G','int');
				$txnid = getGP('txnid','G','int');
				$orders = $_G['db']->fetch_one_array('SELECT o.*,s.firstname,s.phone,s.street,s.shipping_fee FROM'.table('orders').'o LEFT JOIN'.table('orders_shipping').'s ON o.txnid=s.txnid WHERE o.txnid="'.$txnid.'" AND o.uid="'.$_G['uid'].'"');
				//权限检查
				if(!$id || !$txnid || !$orders['txnid']){
					$return['s'] = false;
					$return['m'] = '';
					print_r(json_encode($return));
					exit;
				}
				$products = $_G['db']->fetch('orders_product','*', array('txnid' =>$txnid,'pid'=>$id));
				$good = $_G['db']->fetch('caidan','price,stock,shopid,pid,title,image',array('status'=>1,'pid'=>$id));
				
				$shop = $_G['db']->fetch('shops','minprice,minnum,extprice',array('shopid'=>$good['shopid']));
					if($orders['gross'] - $products['price']* $products['qty']<$shop['minprice']){
						$return['s'] = false;
						$return['m'] = '操作失败，原因：将导致订单总价少于最少起送价'.$shop['minprice'].'元';
					}else{
				
						$_G['db']->query('DELETE FROM '.table('orders_product').'WHERE pid="'.$id.'" AND txnid="'.$txnid.'"');
						$_G['db']->update('orders',array('gross'=>$orders['gross'] - $products['price'] * $products['qty']),array('txnid'=>$txnid));
						
						$return['s'] = true;
						$return['q'] = 0;
						$return['p'] = 0;
						$return['t'] = $orders['gross'] - $products['price'] * $products['qty'];
					}
				print_r(json_encode($return));
				exit;
				break;	
			case 'edit':
				$id = getGP('id','G','int');
				if(!$id){
					exit('access deny');
				}
				$orders = $_G['db']->fetch_one_array('SELECT o.*,s.firstname,s.phone,s.street,s.shipping_fee,s.shipping_note,s.shipping_time,s.shipping_note,s.shipping_time FROM'.table('orders').'o LEFT JOIN'.table('orders_shipping').'s ON o.txnid=s.txnid WHERE o.txnid="'.$id.'" AND o.uid="'.$_G['uid'].'"');
				if(!$orders['txnid']){
					exit('access deny');
				}
				if($orders['status']>=1){
					error('订单状态不允许再调整');
				}
				$products = $_G['db']->fetch_all('SELECT * FROM'.table('orders_product')."WHERE txnid ='".$id."'");
				break;
			case 'save':
				$id = getGP('id','P','int');
				if(!IS_POST || !$id){
					error('拒绝访问');
				}
				//归属检查
				$orders = $_G['db']->fetch('orders','txnid',array('txnid'=>$id,'uid'=>$_G['uid']));
				if(!$orders['txnid']){
					error('拒绝访问');
				}
				$txn = array(
					'phone' => getGP('phone','POST'),
					'street' => getGP('address','POST'),
					'shipping_time' => getGP('deliver_time','POST'),
					'shipping_note' => getGP('note','POST'),
				);
				if(!is_phone($txn['phone'])){
					error('手机号错误');
				}
				if(strlen($txn['street'])<4){
					error('地址错误');
				}
				$_G['db']->update("orders_shipping",$txn,array('txnid'=>$id));
				
				error('操作成功');
				
				
				break;
			case 'quick':
				break;	
			case 'view':
				$id = getGP('id','G','int');
				if(!$id){
					error('参数错误');
				}
				//$orders = $_G['db']->fetch('orders','txnid',array('txnid'=>$id,'uid'=>$_G['uid']));
				
				$data = $_G['db']->fetch_one_array("SELECT o.*,s.firstname,s.phone,s.street,s.shipping_fee,s.zip,s.type as shipping_type FROM".table('orders')."o LEFT JOIN".table('orders_shipping')."s ON o.txnid=s.txnid WHERE o.uid='".$_G['uid']."'  AND o.txnid=$id LIMIT 1");
				$goods = $_G['db']->fetch_all('SELECT * FROM'.table('orders_product').'WHERE txnid ='.$id);
				
				if(!$data['txnid']){
					error("订单不存在");
				}
				if($data['type']=='pointpay'){
					if($_G['settings']['point']['pointratio'])$ratio = 1/$_G['settings']['point']['pointratio'];
					$setting = unserialize(decrypt($_G['settings']['point']['pointsetting']));
				}
				
				break;
			default:
				$limit = 15;
				$caidans = $datas = $ids = array();
				$field = getGP('search_field','P');
				loadialog();
				$where = '';
				if($field) {
					$where = " AND (o.txnid='".$field."' OR s.phone LIKE '%".$field."%') ";
				}
				$sql = 'SELECT o.*,s.firstname,s.phone,s.street,s.shipping_fee FROM'.table('orders').'o LEFT JOIN'.table('orders_shipping').'s ON o.txnid=s.txnid WHERE o.uid="'.$_G['uid'].'"'.$where.' ORDER BY o.txnid DESC';
				$query = $_G['db']->page_query($sql,$limit);
				$num = $_G['db']->result('SELECT count(o.txnid) FROM'.table('orders').'o LEFT JOIN'.table('orders_shipping').'s ON o.txnid=s.txnid WHERE o.uid="'.$_G['uid'].'"'.$where.' ORDER BY o.txnid DESC');
				while($data = $_G['db']->fetch_array($query)) {
					$datas[] = $data ;
					$ids[] = $data['txnid'];
				}
				if(!empty($ids)){
				$query = $_G['db']->query('SELECT * FROM'.table('orders_product').'WHERE txnid IN('.implode(',', $ids).')');
				while($data = $_G['db']->fetch_array($query)){
					$caidans[$data['txnid']][] = $data;
				}
				}
				$pager = pager($num,$limit);

		}
		
		include template('uc/myorder');
	}
	
}	